<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="resource/js/jquery-1.7.1.js"></script>
</head>
<body>
<h2>Hello World!</h2>
<form >
    用户名：<input type="text" name="username"id="username" >
    密码：<input type="password" name="password" id="password">
    确认密码：<input type="password" name="password" id="confirm" onblur="c()">
    身份证号：<input type="text" name="idcard" id="idcard" onblur=" i()">
    电话：<input type="text" name="phone" id="phone"onblur=" p()">

    <input type="button" id="button" value="提交"></input>
</form>

<script>

    $("#button").click(function () {

        if($("#username").val()==""||$("#password").val()==""||$("#confirm").val()==""||$("#idcard").val()==""||
            $("#phone").val()==""){
            alert("信息不能为空，请重新输入");
        }else{
            $.ajax({
                    url:"/adduser",
                    data:{
                        username:$("#username").val(),
                        password:$("#password").val(),
                        idcard:$("#idcard").val(),
                        phone:$("#phone").val()
                    },
                    type:"post",
                    success:function (data) {
                        if(data==null){
                            location.href="/login.jsp";
                        }else{
                            alert("给身份证号或电话已经绑定账号，一个身份证号与电话号码只允许绑定一个账号")
                        }
                    }
                }
            )
        }
    })

</script>
<script >
    var phoneNumReg = /^1[3|4|5|7|8]\d{9}$/
    function p() {
        if(!phoneNumReg.test(document.getElementById("phone").value)&&(document.getElementById("phone").value)!="") {
            alert('手机号码有误，请重填，手机号码11位数字');
            document.getElementById("phone").value="";
        }
    }

    var idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    function i() {
        if(!idCardRegex.test(document.getElementById("idcard").value)&&(document.getElementById("idcard").value)!="") {
            alert('身份证号有误，请重新输入');
            document.getElementById("idcard").value="";
        }
    }

    function c() {
        var password = document.getElementById("password").value;
        var confirm = document.getElementById("confirm").value;
        console.log(password);
        console.log(confirm);
        if(password!=confirm && confirm !=""){
            alert("前后密码不一致，请重新输入");
            document.getElementById("confirm").value="";
        }
    }

</script>
</body>
</html>


